package com.example.chesswithnet.Controller;

import com.example.chesswithnet.Dao.UserDao;
import com.example.chesswithnet.Entity.User;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;

public class Login extends BaseController {

    @FXML
    private TextField account;
    @FXML
    private PasswordField password;
    @FXML
    private Button btnLogin;
    private String accountText;
    private String passwordText;
    Alert alert;


    public void onRegClick() throws IOException {
        newStage("../register.fxml",null,"注册");
    }

    public void onBtnLoginClick() throws IOException {
        accountText = account.getText();
        passwordText = password.getText();
        if(accountText.trim().isEmpty()){
            alert(Alert.AlertType.WARNING,"账号不能为空");
            return;
        }else if(passwordText.trim().isEmpty()){
            alert(Alert.AlertType.WARNING,"密码不能为空");
            return;
        }
        User user = new UserDao().login(accountText,passwordText);
        if(user != null){
            //跳转到中国象棋
            alert(Alert.AlertType.INFORMATION,"登陆成功");
            newStage("../chess.fxml",new chessController(),"中国象棋");
            //关闭登录界面
            Stage stage1 = (Stage) btnLogin.getScene().getWindow();
            stage1.close();
        }else
            alert(Alert.AlertType.ERROR,"账号或密码错误");
    }

    public void onBtnForgetPassClick() throws IOException {
        newStage("../forgetPass.fxml",new ForgetPass(),"忘记密码");
    }

}
